<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/26
 * Time: 11:36
 */

namespace app\api\controller;


use app\api\service\Goods;
use app\common\Code;
use app\common\model\Community;
use app\common\model\GoodsCategory;
use app\common\model\GoodsCategoryRelation;
use app\common\model\Sellers;
use app\common\model\UserCart;
use app\common\Session;

class V1
{
    /**
     * 全部商家
     * 参数
     * community_id
     */
    function shop_list()
    {
        $param = input();
        $cond = [];
        $shops = [];
        if (isset($param["community_id"])) {
            $cond["shop_community_seller_relation.community_id"] = $param["community_id"];
            $shops = Sellers::all(function ($query) use ($cond) {
                $query->join("shop_community_seller_relation", "shop_sellers.id = shop_community_seller_relation.seller_id")
                    ->where($cond);
            });
        }
        response_json(0, $shops);
    }

    /**
     * 搜索
     * 参数
     * search 搜索的内容  非必须
     * community_id  最好保存到stroge 社区id   必须
     * seller_id  店铺id  非必须
     * goods_category_id  分类id  非必须
     * sort_name 排序字段 id price sale     非必须
     * sort_type 排序方式 1 升序  2 降序    非必须
     */
    public function search()
    {
        $param = input();
        $cond = [];
        if (isset($param["search"]) && !empty($param["search"])) {
            $cond["goods_name"] = ["LIKE", "%" . $param["search"] . "%"];
        }
        if (isset($param["community_id"])) {
            $cond["shop_community_seller_relation.community_id"] = $param["community_id"];
        }
        if (isset($param["seller_id"]) && !empty($param["seller_id"])) {
            $cond["shop_goods.seller_id"] = $param["seller_id"];
        }
        $injection = function (\app\common\model\Goods $query) use ($param) {
            //排序
            if (isset($param["sort_name"]) && in_array($param["sort_name"], [
                    "id",
                    "price",
                    "sale"
                ])
            ) {
                $sort_name = $param["sort_name"];
                $sort_name = $sort_name == "id" ? "shop_goods.id" : $sort_name;
                $sort_type = isset($param["sort_type"]) ? $param["sort_type"] : 2;
                $sort_type = $sort_type == 2 ? "DESC" : "ASC";
                $order = sprintf("%s %s", $sort_name, $sort_type);
                $query->order($order);
            }
            //分类
            if (isset($param["goods_category_id"]) && !empty($param["goods_category_id"])) {
                $query->join("shop_goods_category_relation", "shop_goods.id = shop_goods_category_relation.goods_id");
                $query->where([
                    "shop_goods_category_relation.category_id" => $param["goods_category_id"]
                ]);
            }
        };
        $goods = Goods::list_($cond, $injection);
        response_json(0, $goods);
    }

    /**
     * 商品详情
     * 参数
     * goods_id
     * community_id  最好保存到stroge 社区id
     */
    public function good()
    {
        $param = input();
        $cond = [];
        if (isset($param["goods_id"])) {
            $cond["shop_goods.id"] = $param["goods_id"];
        }
        if (isset($param["community_id"])) {
            $cond["community_id"] = $param["community_id"];
        }
        $goods = Goods::good($cond);
        if (isset($goods->image)) {
            $goods->images = explode(";", $goods->image);
        }
        if (isset($goods->content)) {
            $goods->content = str_replace(" src=", " src='#' data-img_url=", $goods->content);
        }
        if (empty($goods)) {
            response_json(Code::GOODS_NOT_EXISTS);
        }
        response_json(0, $goods);
    }

    /**
     * 返回全部分类
     */
    public function category()
    {
        $cate = GoodsCategory::all();
        $tree = [];
        foreach ($cate as $k => $v) {
            $tree[$v->id] = $v->toArray();
        }
        response_json(0, category_to_tree($tree));
    }

    /**
     * 获取用户信息
     */
    public function user_info()
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user->fund = $user->getUserFund();
        response_json(0, $user);
    }

    /**
     * 搜索社区
     * county_id 比如 370203
     * city_id 比如 370200
     * province_id 比如 370000
     * search 比如 中海国际
     */
    public function search_community_list()
    {
        $param = input();
        $cond = [];
        if (isset($param["county_id"])) {
            $cond["county_id"] = $param["county_id"];
        }
        if (isset($param["city_id"])) {
            $cond["city_id"] = $param["city_id"];
        }
        if (isset($param["province_id"])) {
            $cond["province_id"] = $param["province_id"];
        }
        if (isset($param["search"])) {
            $cond["community_name"] = ["LIKE", "%" . $param["search"] . "%"];
        }
        $community = Community::all($cond);
        response_json(0, $community);
    }


    /**
     * 分类下的商品
     * community_id  最好保存到stroge 社区id
     * cate_id     分类id
     */
    public function cate_goods()
    {
        $param = input();
        $cond = [];
        if (isset($param["community_id"])) {
            $cond["community_id"] = $param["community_id"];
        }
        if (isset($param["cate_id"])) {
            $cond["shop_goods_category_relation.id"] = $param["cate_id"];
        }
        $goods = Goods::cate_($cond);
        response_json(0, $goods);
    }

    /**
     * 添加购物车
     * community_id  最好保存到stroge 社区id
     * goods_id
     * count
     */
    public function cart_add($goods_id = 0, $count = 0, $community_id = 0)
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user_id = $user->id;
        $cart = new \app\common\model\UserCart();
        /** @var \app\common\model\UserCart $cart_ */
        $cart_ = $cart->get(["goods_id" => $goods_id, "community_id" => $community_id, "user_id" => $user_id]);
        if (!empty($cart_)) {
            $cart = $cart_;
            if ($cart->count + $count >= 0) {
                $cart->setInc("count", $count);
            }
        } else {
            $cart->user_id = $user_id;
            $cart->community_id = $community_id;
            $cart->goods_id = $goods_id;
            $cart->count = $count;
        }
        if ($cart->count) {
            $cart->save();
        }
        response_json(0, $cart);
    }

    /**
     * 用户购物车
     * community_id  最好保存到stroge 社区id
     */
    public function user_cart($community_id = 0)
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $user_id = $user->id;
        $cart = new \app\common\model\UserCart();
        $cond = ["shop_user_cart.user_id" => $user_id, "community_id" => $community_id];
        /** @var \app\common\model\UserCart $cart_ */
        $cart_ = $cart->all(function ($query) use ($cond) {
            $query
                ->field("shop_user_cart.id as cart_id,shop_user_cart.count,shop_sellers.*,shop_goods.*,shop_goods.id as goods_id")
                ->join("shop_goods", "shop_goods.id = shop_user_cart.goods_id")
                ->join("shop_sellers", "shop_sellers.id = shop_goods.seller_id")
                ->where($cond);
        });
        response_json(0, $cart_);
    }

    /**
     * 删除购物车
     * 参数
     *    goods_id
     * @param int $goods_id
     */
    function del_cart($goods_id = 0)
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        UserCart::destroy(["goods_id" => $goods_id, "user_id" => $user->id]);
        response_json(Code::NONE);
    }

    /**
     * 用户购物车上标数量  、
     * 参数
     *    community_id  传值返回数量   不传值返回
     * @param $community_id
     */
    public function user_cart_count($community_id = 0)
    {
        $user = Session::get_local_user();
        if (empty($user)) {
            response_json(Code::NO_LOGIN);
        }
        $cart = new \app\common\model\UserCart();
        $sum = $cart
            ->join("shop_goods", "shop_goods.id = shop_user_cart.goods_id")
            ->join("shop_sellers", "shop_sellers.id = shop_goods.seller_id")
            ->where([
                "shop_user_cart.community_id" => $community_id,
                "shop_user_cart.user_id" => $user->id
            ])->sum("count");
        response_json(Code::NONE, [
            "cart_sum" => $sum
        ]);
    }
}